package com.nuoniu.sibanyun.mapper.finance;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.entity.finance.VAccountingAuxiliaryLedger;
import com.nuoniu.sibanyun.entity.finance.VAccountingSearchProgramme;
import com.nuoniu.sibanyun.entity.finance.VAccountingVoucherDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 辅助总账表 Mapper 接口
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-25
 */
@Component
@Mapper
public interface VAccountingAuxiliaryLedgerMapper extends BaseMapper<VAccountingAuxiliaryLedger> {

    /**
     * 检测是否已记账
     * @author 小啊刚
     * @date 2021-05-14
     */
    Integer checkAccounting(VAccountingAuxiliaryLedger ledger);

    /**
     * 根据客户获取
     * @author 小啊刚
     * @date 2021-05-14
     */
    @Select("select * from fag_acc_auxiliary_ledger where crm_id=#{crmId} and account_year=#{accountYear} and account_period=#{accountPeriod}")
    VAccountingAuxiliaryLedger getByCrm(Integer crmId, Integer accountYear, Integer accountPeriod);

    /**
     * 根据个人获取
     * @author 小啊刚
     * @date 2021-05-14
     */
    @Select("select * from fag_acc_auxiliary_ledger where user_id=#{userId} and account_year=#{accountYear} and account_period=#{accountPeriod}")
    VAccountingAuxiliaryLedger getByUser(Integer userId, Integer accountYear, Integer accountPeriod);

    /**
     * 根据供应商
     * @author 小啊刚
     * @date 2021-05-14
     */
    @Select("select * from fag_acc_auxiliary_ledger where vendor_id=#{vendorId} and account_year=#{accountYear} and account_period=#{accountPeriod}")
    VAccountingAuxiliaryLedger getByVendor(Integer vendorId, Integer accountYear, Integer accountPeriod);

    /**
     * 获取辅助总账详情
     * @author 小啊刚
     * @date 2021-05-14
     */
    VAccountingAuxiliaryLedger getAuxiliaryLedger(VAccountingVoucherDetail voucherDetail);

    /**
     * 获取上一期间
     * @author 小啊刚
     * @date 2021-05-14
     */
    VAccountingAuxiliaryLedger getUpAuxiliaryLedger(VAccountingVoucherDetail voucherDetail);

    @Select("select * from fag_acc_auxiliary_ledger where accounting_id=#{accountingId} and account_year=#{accountYear} and account_period=#{period}")
    VAccountingAuxiliaryLedger getById(Integer accountingId, Integer accountYear, int period);

    /**
     * 获取客户辅助总账余额
     * @author 小啊刚
     * @date 2021-05-15
     */
    Page<VAccountingAuxiliaryLedger> pageCrmAccountingAuxiliaryLedger(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    /**
     * 获取客户辅助总账余额
     * @author 小啊刚
     * @date 2021-05-15
     */
    Page<VAccountingAuxiliaryLedger> pageVendorAccountingAuxiliaryLedger(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    /**
     * 获取客户辅助总账余额
     * @author 小啊刚
     * @date 2021-05-15
     */
    Page<VAccountingAuxiliaryLedger> pageUserAccountingAuxiliaryLedger(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    /**
     * 获取辅助期初
     * @author 小啊刚
     * @date 2021-05-17
     */
    Page<VAccountingAuxiliaryLedger> pageAuxiliaryBeginList(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    @Select("select begin_amount from fag_acc_ledger where account_year=#{accountYear} and accounting_id=#{accountingId} order by account_period limit 1")
    Integer getAccountingBegin(Integer accountYear, Integer accountingId);

    /**
     * 获取余额明细列表
     * @author 小啊刚
     * @date 2021-05-19
     */
    Page<VAccountingAuxiliaryLedger> pageAccountingAuxiliaryLedgerList(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    @Select("SELECT accounting_id, sum(begin_amount) begin_amount FROM fag_acc_auxiliary_ledger WHERE company_id=#{companyId} and account_year = #{accountYear} and account_period=1 group by accounting_id")
    List<VAccountingAuxiliaryLedger> getAuxiliaryLedgerList(Integer companyId, Integer accountYear);

    @Select("select count(1) from fag_voucher where company_id=#{companyId} and status=3 and account_year=#{accountYear}")
    Integer checkAccountings(Integer companyId, Integer accountYear);

    /**
     * 项目辅助总账余额
     * @author 小啊刚
     * @date 2022-05-18
     */
    Page<VAccountingAuxiliaryLedger> pageProjectAccountingAuxiliaryLedger(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    /**
     * 部门辅助总账余额
     * @author 小啊刚
     * @date 2022-05-18
     */
    Page<VAccountingAuxiliaryLedger> pageOfficeAccountingAuxiliaryLedger(Page page, @Param("pro") VAccountingSearchProgramme searchProgramme);

    /**
     * 辅助总账
     * @author 小啊刚
     * @date 2022-05-19
     */
    List<VAccountingAuxiliaryLedger> accountingAuxiliaryDetailList(VAccountingSearchProgramme searchProgramme);
}
